/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.pack.segment.param;

import net.kyou.exception.KyouErr;
import net.kyou.exception.KyouException;

import org.junit.Assert;
import org.junit.Test;

public class ParamTest {
    @Test
    public void test() {
        String expr;
        Param param;

        expr = "1";
        param = Param.__new(expr, null, null);
        Assert.assertTrue(param instanceof PlainIntParam);

        expr = "n";
        param = Param.__new(expr, null, null);
        Assert.assertTrue(param instanceof NameParam);

        expr = "i2s 1";
        param = Param.__new(expr, null, null);
        Assert.assertTrue(param instanceof ConvertI2SParam);
        Assert.assertEquals("1", param.__eval(null));

        expr = "i2s.16 15";
        param = Param.__new(expr, null, null);
        Assert.assertTrue(param instanceof ConvertI2SParam);
        Assert.assertEquals("f", param.__eval(null));
        
        expr = "lens i2s 123456";
        param = Param.__new(expr, null, null);
        Assert.assertTrue(param instanceof LenSParam);
        Assert.assertEquals(6, param.__eval(null));
    }

    @Test
    public void testEx() {
        try {
            Param.__new(null, null, null);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.InitParamFail, ex.err);
            Assert.assertEquals(KyouErr.Style.NullParamExpr, ((KyouException) ex.cause).err);
        }
        try {
            Param.__new("", null, null);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.InitParamFail, ex.err);
            Assert.assertEquals(KyouErr.Style.NullParamExpr, ((KyouException) ex.cause).err);
        }
        try {
            Param.__new("1-1", null, null);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.InitParamFail, ex.err);
        }
        try {
            Param.__new("xxx", null, null);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.InitParamFail, ex.err);
            Assert.assertEquals(KyouErr.Style.InvalidParamTag, ((KyouException) ex.cause).err);
        }
    }
}
